<template>
  <div class="box">
    <div class="box1">
      <div class="word">教学评价系统</div>
      <v-form
        ref="form"
        lazy-validation
        style="
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          align-item: center;
          height: 50vh;
          width: 20vw;
        "
      >
        <v-text-field
          v-model="loginForm.username"
          :rules="[(v) => !!v || 'ID is required']"
          label="StudentID"
          required
          style="margin-top: 3vh"
        ></v-text-field>

        <v-text-field
          v-model="loginForm.password"
          :type="'password'"
          :rules="[(v) => !!v || 'Password is required']"
          label="Password"
          required
          style="margin-top: 3vh"
        ></v-text-field>

        <div class="forget" @click="toChange">忘记密码</div>
 
        <v-snackbar v-model="alert_err" centered
          >您输入的账号或密码错误</v-snackbar
        >
        <v-snackbar v-model="alert_time" centered
          >请求异常，请稍后重试</v-snackbar
        >

        <v-btn
          style="height: 6vh; margin: 6vh auto; width: 20vw"
          color="info"
          :loading="loading"
          :disabled="loading"
          @click="onSubmit"
        >
          LOGIN
        </v-btn>
      </v-form>
    </div>
  </div>
</template>

<script>
import { instance } from "../api/request";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      alert_err: false,
      loading: false,
      alert_time:false,
    };
  },
  methods: {
    onSubmit() {
      this.loading = true;
      let jsonVal = JSON.stringify(this.loginForm);
      instance({
        url: "/student/login",
        method: "post",
        headers: {
          //请求头设置为表单提交的请求头
          "Content-Type": "application/json",
        },
        data: jsonVal,
      }).then((res) => {
        if (res.data.data !== null) {
          this.$store.commit("clearContain");
          let userinfo = res.data.data;
          localStorage.setItem("userinfo", JSON.stringify(userinfo));
          this.$store.commit("getStudentInfo", userinfo);
          this.$router.push("/main");
        } else {
          this.alert_err = true;
          setTimeout(() => {
            this.alert_err = false;
          }, 1000);
        }
        this.loading = false;
      })
      .catch(()=>{
        this.alert_time=true
         setTimeout(() => {
            this.alert_time = false;
            this.loading=false;
          }, 1000);
      });
    },
    toChange() {
      alert("该功能暂未上线");
    },
  },
};
</script>

<style scoped>
.box {
  display: flex;
  width: 100%;
  height: 100vh;
  background: url("../assets/login_bg.png") top left no-repeat;
  background-size: 100vw 100vh;
  justify-content: center;
  align-items: center;
}
.box1 {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  flex-direction: column;
  width: 26vw;
  height: 70vh;
  margin: 0 auto;
  background: #fff;
  padding: 20px 40px;
  border-radius: 10px;
  box-shadow: 0px 0px 30px 5px #eeeeee;
}
.forget {
  margin-left: 13vw;
  font-size: 14px;
  color: #808080;
  text-decoration: underline;
  cursor: pointer;
}
.word {
  margin: 6vh auto;
  width: 100%;
  height: 3vh;
  text-align: center;
  line-height: 3vh;
  font-size: 26px;
  color: #0076f6;
  letter-spacing: 6px;
}
</style>
